//
//  RecomendController.m
//  We_Travel
//
//  Created by lanou3g on 15/12/16.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "RecomendController.h"
#import "MyArrangeCell.h"
#import "RecomendViewCell.h"
#import "TravelDayController.h"
#import "url.h"
#import "SiteManager.h"
#import "NH_DataDownloadTools.h"
#import "RecomendCellModel.h"
#import "CoreDataToolHelper.h"

@interface RecomendController ()
@property (nonatomic,strong) NSMutableArray *dataArray;
@end

@implementation RecomendController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back.png"] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarButtonItemAction:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    [self.tableView registerNib:[UINib nibWithNibName:@"MyArrangeCell" bundle:nil] forCellReuseIdentifier:@"myArrange"];
    [self.tableView registerNib:[UINib nibWithNibName:@"RecomendViewCell" bundle:nil] forCellReuseIdentifier:@"recomend"];
    [self requireData];
}
- (void)leftBarButtonItemAction:(UIBarButtonItem *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)requireData{
    [NH_DataDownloadTools downloadDataWithURL:[NSString stringWithFormat:HT_RecomendUrl,self.idUrl] andMethod:@"get" andBody:nil andBlock:^(NSData *data) {
        NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        self.dataArray = [NSMutableArray array];
        for (NSDictionary *dic in array) {
            RecomendCellModel *model = [[RecomendCellModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        [self.tableView reloadData];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//#warning Incomplete implementation, return the number of rows
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        MyArrangeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myArrange"];
        return cell;
    }
    else{
        RecomendCellModel *model = self.dataArray[indexPath.row-1];
        RecomendViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"recomend"];
        cell.model = model;
        return cell;
    }
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds) - 10, 49)];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 49)];
    label.text = @"请选择玩法";
    [headerView addSubview:label];
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 49;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 49;
    }else{
    RecomendCellModel *model = self.dataArray[indexPath.row];
    NSString *str = model.description1;
    CGSize size = CGSizeMake(CGRectGetWidth([UIScreen mainScreen].bounds) - 40, 2000);
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
    CGRect rect = [str boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil];
      //  NSLog(@"%lf",rect.size.height);
    return rect.size.height + 125;
    }
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        TravelDayController *tdv = [[TravelDayController alloc]init];
        tdv.isRecomend = NO;
        tdv.title = [NSString stringWithFormat:@"%@第%ld天行程安排",[SiteManager shareSiteManager].areaNameCurrent,[SiteManager shareSiteManager].dayCountCurrent+1];
        [self.navigationController showViewController:tdv sender:nil];
    }else{
        RecomendCellModel *model = self.dataArray[indexPath.row - 1];
        TravelDayController *tvc = [[TravelDayController alloc] init];
        tvc.title = [NSString stringWithFormat:@"%@第%ld天行程推荐",[SiteManager shareSiteManager].areaNameCurrent,[SiteManager shareSiteManager].dayCountCurrent+1];
        tvc.siteID = model.ID;
        tvc.siteDay = self.siteDay;
        tvc.areaCount = self.areaCount;
        tvc.isRecomend = YES;
        [self.navigationController showViewController:tvc sender:nil];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
